package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.PAudio;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.*;
import org.osgl.util.S;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@Controller("audio")
public class PAudioController extends CookieService{

    @Inject
    private PAudio.Mapper pAudioMapper;

    @Description("我的音频列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PAudio> audioList(
            String artid,
            String paname,
            String pastatus,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PAudio> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        map.put("coid", me.getCoid());
        map.put("artid", artid);
        map.put("paname", paname);
        map.put("pastatus", pastatus);
        query.setParas(map);
        pAudioMapper.getSQLManager().pageQuery("pAudio.select_audio_page_list", PAudio.class, query);
        return query;
    }

    @Description("音频--音频详情")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PAudio getPArticle(@Description("音频id") String paid) {
        if (S.isEmpty(paid)) {
            return new PAudio();
        }
        return pAudioMapper.single(paid);
    }

    @Description("音频--音频保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePAudio(@Description("音频对象") PAudio pAudio) {
        pAudio.setCoid(me.getCoid());
        if (S.isNotBlank(pAudio.getPaid())) {
            pAudioMapper.updateTemplateById(pAudio);
            return pAudio.getPaid();
        } else {
            String paid = S2.UUID();
            pAudio.setPaid(paid);
            pAudioMapper.insert(pAudio);
            return paid;
        }
    }

    @Description("音频--音频状态修改")
    @With({ICLogin.class, ITransacte.class})
    @PutAction("updateStatus.json")
    public String updatePAudio(@Description("音频id") String paid,@Description("音频状态") String pastatus) {
        PAudio pAudio = new PAudio();
        pAudio.setPaid(paid);
        pAudio.setPastatus(pastatus);
        pAudioMapper.updateTemplateById(pAudio);
        return pAudio.getPaid();
    }

    @Description("音频--音频删除")
    @With({ICLogin.class, ITransacte.class})
    @DeleteAction("{paid}/del.json")
    public Integer delPAudio(@Description("音频id") String paid) {
        return pAudioMapper.deleteById(paid);
    }


}
